package logic

import (
	"context"

	"lottery-zero/internal/model"
	"lottery-zero/internal/svc"
	"lottery-zero/internal/types"

	"github.com/tal-tech/go-zero/core/logx"
)

type UserNameExistLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserNameExistLogic(ctx context.Context, svcCtx *svc.ServiceContext) UserNameExistLogic {
	return UserNameExistLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserNameExistLogic) UserNameExist(req types.UserNameGet) (*types.UserNameResponse, error) {
	user := model.UserAuth{}
	result := l.svcCtx.PGDB.Where(&model.UserAuth{Name: req.Username}).First(&user)
	if result.RowsAffected > 0 {
		return &types.UserNameResponse{Exist: true}, nil
	}
	return &types.UserNameResponse{Exist: false}, nil
}
